package com.innovations.inn.service.impl;

import com.innovations.common.utils.uuid.IdGeneratorSnowflake;
import com.innovations.inn.domain.ResearchInstitution;
import com.innovations.inn.mapper.ResearchInstitutionMapper;
import com.innovations.inn.service.IResearchInstitutionService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 科研机构Service业务层处理
 *
 * @author dp
 * @date 2024-08-09
 */
@Service
public class ResearchInstitutionServiceImpl implements IResearchInstitutionService {
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;
    @Autowired
    private ResearchInstitutionMapper researchInstitutionMapper;

    /**
     * 查询科研机构
     *
     * @param institutionId 科研机构主键
     * @return 科研机构
     */
    @Override
    public ResearchInstitution selectResearchInstitutionByInstitutionId(Long institutionId) {
        return researchInstitutionMapper.selectResearchInstitutionByInstitutionId(institutionId);
    }

    /**
     * 查询科研机构列表
     *
     * @param researchInstitution 科研机构
     * @return 科研机构
     */
    @Override
    public List<ResearchInstitution> selectResearchInstitutionList(ResearchInstitution researchInstitution) {
        return researchInstitutionMapper.selectResearchInstitutionList(researchInstitution);
    }

    /**
     * 新增科研机构
     *
     * @param researchInstitution 科研机构
     * @return 结果
     */
    @Override
    public int insertResearchInstitution(ResearchInstitution researchInstitution) {
        return researchInstitutionMapper.insertResearchInstitution(researchInstitution);
    }

    /**
     * 修改科研机构
     *
     * @param researchInstitution 科研机构
     * @return 结果
     */
    @Override
    public int updateResearchInstitution(ResearchInstitution researchInstitution) {
        return researchInstitutionMapper.updateResearchInstitution(researchInstitution);
    }

    /**
     * 批量删除科研机构
     *
     * @param institutionIds 需要删除的科研机构主键
     * @return 结果
     */
    @Override
    public int deleteResearchInstitutionByInstitutionIds(Long[] institutionIds) {
        return researchInstitutionMapper.deleteResearchInstitutionByInstitutionIds(institutionIds);
    }

    /**
     * 删除科研机构信息
     *
     * @param institutionId 科研机构主键
     * @return 结果
     */
    @Override
    public int deleteResearchInstitutionByInstitutionId(Long institutionId) {
        return researchInstitutionMapper.deleteResearchInstitutionByInstitutionId(institutionId);
    }

    /**
     * 根据机构名称查询机构标识
     *
     * @param institutionName 机构名称
     * @return 机构标识
     */
    @Override
    public Long selectResearchInstitutionIdByName(String institutionName) {
        ResearchInstitution researchInstitution = new ResearchInstitution();
        researchInstitution.setInstitutionName(institutionName);
        List<ResearchInstitution> researchInstitutionList = researchInstitutionMapper.selectResearchInstitutionList(researchInstitution);
        if (researchInstitutionList == null || researchInstitutionList.isEmpty()) {
            return null;
        } else {
            return researchInstitutionList.get(0).getInstitutionId();
        }
    }

    /**
     * 实体数据补全
     *
     * @param researchInstitution 实体
     * @param userId              用户id
     * @param crateType           创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(ResearchInstitution researchInstitution, Long userId, Integer crateType) {
        //标识
        if (researchInstitution.getInstitutionId() == null || "".equals(researchInstitution.getInstitutionId())) {
            researchInstitution.setInstitutionId(idGeneratorSnowflake.nextId());
        }

        //创建人、创建时间
        if (crateType == 1) {
//            if (researchInstitution.getCreateBy() == null || "".equals(researchInstitution.getCreateBy())) {
//                researchInstitution.setCreateBy(userId);
//            }
//            if (researchInstitution.getCreateTime() == null) {
//                researchInstitution.setCreateTime(DateUtils.getNowDate());
//            }
        }else if (crateType == 2) {
            if (researchInstitution.getPhone() == null || "".equals(researchInstitution.getPhone())){
                throw new RuntimeException("电话号码不能为空");
            }else {
//                // 电话号码验证
//                String regex = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
//                boolean matches = researchInstitution.getPhone().matches(regex);
//                if (!matches){
//                    throw new RuntimeException("电话号码格式不正确");
//                }
            }
        }
    }
}
